using AutoMapper;
using Domain.Material_Management;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Material_Management;
using MES.Server.API.Dto.Material_Management.OutPut;
using ReturnValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MES.Server.API.Application.Handler.Material_Management
{
    /// <summary>
    /// 获取待办物料申请单处理程序
    /// </summary>
    public class GetPendingMaterialAskHandler : IRequestHandler<GetPendingMaterialAskCommand, ApiResult<List<GetMaterialAskOutPut>>>
    {
        private readonly IRepository<MaterialAskModel> materialAskRepo;
        private readonly IRepository<ProductMaterialsModel> productMaterialsRepo;

        public GetPendingMaterialAskHandler(
            IRepository<MaterialAskModel> materialAskRepo,
            IRepository<ProductMaterialsModel> productMaterialsRepo)
        {
            this.materialAskRepo = materialAskRepo;
            this.productMaterialsRepo = productMaterialsRepo;
        }

        /// <summary>
        /// 获取待办物料申请单列表
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>待办物料申请单列表</returns>
        public async Task<ApiResult<List<GetMaterialAskOutPut>>> Handle(GetPendingMaterialAskCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<List<GetMaterialAskOutPut>>();

            try
            {
                // 查询所有状态为指定状态的物料申请单
                var materialAsk = materialAskRepo.GetAll()
                    .Where(m => m.MaterialAskStatus == request.Status && m.IsDel == false)
                    .OrderByDescending(m => m.CreateTime);

                var productMaterials = productMaterialsRepo.GetAll().Where(x => x.IsDel == false);

                var linq = from a in materialAsk
                          join b in productMaterials on a.ProductMaterialsId equals b.Id into ab
                          from b in ab.DefaultIfEmpty()
                          select new GetMaterialAskOutPut
                          {
                              Id = a.Id,
                              MaterialAskCode = a.MaterialAskCode,
                              ProductMaterialsId = b != null ? b.Id : 0,
                              ProductMaterialsName = b != null ? b.ProductMaterialsName : string.Empty,
                              MaterialAskCount = a.MaterialAskCount,
                              MaterialAskUnit = a.MaterialAskUnit,
                              MaterialAskDesc = a.MaterialAskDesc,
                              MaterialAskStatus = a.MaterialAskStatus,
                              CreateTime = a.CreateTime,
                              CreaterPeople = a.CreaterPeople,
                              UpdateTime = a.UpdateTime
                          };

                var pendingList = linq.ToList();

                if (pendingList != null && pendingList.Any())
                {
                    result.Code = StatusCode.Success;
                    result.Message = "获取待办物料申请单成功";
                    result.Data = pendingList;
                }
                else
                {
                    result.Code = StatusCode.Success;
                    result.Message = "没有待办物料申请单";
                    result.Data = new List<GetMaterialAskOutPut>();
                }
            }
            catch (Exception ex)
            {
                result.Code = StatusCode.Fail;
                result.Message = $"获取待办物料申请单失败: {ex.Message}";
                result.Data = null;
            }

            return await Task.FromResult(result);
        }
    }
}